package com.asp.aspchain.asset.controller;


import com.alibaba.fastjson.JSON;
import com.asp.aspchain.asset.constants.ErrorCode;
import com.asp.aspchain.asset.request.ApplyReq;
import com.asp.aspchain.asset.service.AssetService;
import com.asp.aspchain.core.model.AppBaseModel;
import com.asp.aspchain.core.model.TocAccountModel;
import com.asp.aspchain.core.service.IAppBaseService;
import com.asp.aspchain.core.service.ITocAccountService;
import com.asp.aspchain.core.util.AjaxResult;
import com.asp.aspchain.core.util.BlogUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.Set;

/**
 * 资产接口
 *
 * @author zhongzifeng
 * @date 2018/10/24
 */
@Controller
@RequestMapping("/asset")
public class ApplyAssetController {

	private final static Logger logger = Logger.getLogger(ApplyAssetController.class);
	@Autowired
	private Validator validator;
	@Autowired
	private IAppBaseService appBaseService;
	@Autowired
	private ITocAccountService tocAccountService;
	@Autowired
	private AssetService assetSercice;

	/**
	 * 资产申请接口
	 * @param json
	 * @return
	 */
	@RequestMapping(value = "/applyasset", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult applyasset(@RequestBody String json) {
		logger.info("=============ApplyAsset开始=============");
		logger.info(json);
		BlogUtil.inlog(json, "", "account/applyasset", "applyasset", "req");
		ApplyReq applyReq = null;
		try {
			applyReq = JSON.parseObject(json, ApplyReq.class);
		} catch (Exception e) {
			logger.error("资产申请接口解析错误：" + e.getMessage());
			e.printStackTrace();
		}

		// 参数校验
		AjaxResult result = checkPayParam(applyReq);
		if (result != null) {
			return result;
		}

		// 验证签名
		if (!sign(applyReq.getHmac())) {
			return new AjaxResult(ErrorCode.SIGN_ERROR, "签名验证失败", "签名验证失败");
		}

		AjaxResult ajaxResult = new AjaxResult();
		try{
			ajaxResult = assetSercice.applyasset(applyReq);
		}catch (Exception e) {
			logger.error("applyasset接口错误："+e.getMessage());
			ajaxResult.setTips("系统内部错误");
			ajaxResult.setMessage("系统内部错误");
			ajaxResult.setStatus(ErrorCode.INTERNAL_ERROR);
		}

		String resp = JSON.toJSONString(ajaxResult);
		BlogUtil.inlog(resp, "", "account/crtaccount/", "crtaccount", "resp");
		logger.info("=============AccountHandler结束=============");
		return ajaxResult;
	}

	private AjaxResult checkPayParam(ApplyReq applyReq) {
		// 参数校验
		Set<ConstraintViolation<ApplyReq>> validate = validator.validate(applyReq);
		if (validate.size() > 0) {
			// 如果有错误，取出一条信息返回
			String message = validate.iterator().next().getMessage();
			return new AjaxResult(ErrorCode.PARAM_ERROR, message, message);
		}

		String accountid = applyReq.getAccountid();
		String appmark = applyReq.getAppmark();
		TocAccountModel tocAccount = tocAccountService.selectById(accountid);
		if (tocAccount == null) {
			return new AjaxResult(ErrorCode.ACCOUNT_NOT_EXIST, "用户不存在", "用户不存在");
		}
		AppBaseModel appBase = appBaseService.selectById(tocAccount.getAppid());
		if (appBase == null) {
			return new AjaxResult(ErrorCode.APPID_NOT_EXIST, "appid不存在", "appid不存在");
		}
		if (!appmark.equals(appBase.getAppmark())) {
			return new AjaxResult(ErrorCode.APPMARK_ERROR, "appmark错误", "appmark错误");
		}

		return null;
	}

	private boolean sign(String hmac) {
		// TODO Auto-generated method stub
		return true;
	}

}

